/*
 * Copyright (c) 2010-2011, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef ALKES_GRAPH_UPDATE_CONTEXT_H_INCLUDED_
#define ALKES_GRAPH_UPDATE_CONTEXT_H_INCLUDED_

#include "alkes/graph/internal.h"
#include "alkes/graph/ScissorStack.h"
#include "alkes/gfx/RenderTarget.h"

namespace alkes {

/// \addtogroup scenegraph
//@{

class AL_GRAPH_EXPORT RectStack
{
public:
    RectStack();
    ~RectStack();

    uint16_t getCount() const;
    bool hasDirtyRect() const;
    void addRect(const Rect& rect, uint16_t guard = 0);
    const Rect* getMergedRects(uint16_t& count);
    void shrink(uint16_t count);
    void cancel(uint16_t count);

private:
    bool expand();

private:
    bool merged_;
    uint16_t max_;
    uint16_t count_;
    Rect* rects_;
    uint16_t merged_max_;
    uint16_t merged_count_;
    Rect* merged_rects_;
};

class AL_GRAPH_EXPORT UpdateContext
{
public:
    UpdateContext(RenderTarget* target);
    virtual ~UpdateContext();

    void addOffset(int16_t x, int16_t y);
    void minusOffset(int16_t x, int16_t y);
    void pushScissorSize(int16_t w, int16_t h);
    void popScissorRect();

    const Rect& getCurrentScissorRect();
    RenderTarget* getRenderTarget();
    const Rect& getTargetRect() const;

    bool relayDirtyFlag(bool child_dirty_flag);

    bool setForceDirty(bool force_dirty);
    bool isForceDirty() const;

    void addDirtyRect(const Rect& rect);
    bool hasDirtyRect() const;

    void resetDirtyRect();
    void cancelDirtyRect();
    const Rect* getMergedRects(uint16_t& count);

private:
    UpdateContext(const UpdateContext&);
    void operator=(const UpdateContext&);

private:
    intrusive_ptr<RenderTarget> target_;
    Rect target_rect_;
    ScissorStack scissor_;
    bool force_dirty_;
    TargetBehavior target_behavior_;
    uint16_t history_count_;
    uint16_t history_[2];
    RectStack dirty_rects_;
};

//@}

}

#endif
